

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.activity.MessageActivity;
import com.example.administrator.caigou51.activity.MessageGongGaoActivity;
import com.example.administrator.caigou51.activity.MessageHuoDongActivity;
import com.example.administrator.caigou51.activity.MessageOrderTixingActivity;
import com.example.administrator.caigou51.bean.MessageBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.card.MessageTypeCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;
import com.example.administrator.caigou51.util.ImageUtil;

import java.util.List;


public class MessageTypeCardView extends CardItemView<MessageTypeCard> {
 	private Context mContext;
    private RelativeLayout relativeLayoutHDTZ;
    private ImageView imageViewHdtz;
    private TextView textViewCountHdtz;
    private TextView textViewHdtzDesc;
    private TextView textViewHdtzTime;
    private TextView textViewHdtz;
    private MessageTypeCard card;
    public MessageTypeCardView(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public MessageTypeCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public MessageTypeCardView(Context context,
                                    AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }
    @Override
    public void build(final MessageTypeCard card) {
        // TODO Auto-generated method stub
        super.build(card);
        this.card=card;
        relativeLayoutHDTZ = (RelativeLayout) findViewById(R.id.relativeLayoutHDTZ);
        imageViewHdtz = (ImageView) findViewById(R.id.imageViewHdtz);
        textViewCountHdtz = (TextView) findViewById(R.id.textViewCountHdtz);
        textViewHdtzDesc = (TextView) findViewById(R.id.textViewHdtzDesc);
        textViewHdtzTime = (TextView) findViewById(R.id.textViewHdtzTime);
        textViewHdtz = (TextView) findViewById(R.id.textViewHdtz);

        textViewHdtzDesc.setText(card.getDataEntity().getType_name());
        textViewHdtz.setText(card.getDataEntity().getContent());
        textViewHdtzTime.setText(card.getDataEntity().getSend_time());
        App.displayImageHttpOrFile(card.getDataEntity().getIcon(),imageViewHdtz, ImageUtil.OptionsActivity());
        int unReadNum=Integer.parseInt(card.getDataEntity().getUnread_nums());
        if (unReadNum>0)
        {
            textViewCountHdtz.setText(card.getDataEntity().getUnread_nums());
            textViewCountHdtz.setVisibility(View.VISIBLE);
        }else
        {
            textViewCountHdtz.setVisibility(View.INVISIBLE);
        }
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (MessageActivity.HDTZ_TAG.equals(card.getDataEntity().getType()))
                {

                    Intent intent=new Intent(mContext,MessageHuoDongActivity.class);
                    mContext.startActivity(intent);
                    TaskSetMessageRead(false,MessageActivity.HDTZ_TAG);
                }else if (MessageActivity.DDTX_TAG.equals(card.getDataEntity().getType()))
                {
                    Intent intent=new Intent(mContext,MessageOrderTixingActivity.class);
                    mContext.startActivity(intent);
                    TaskSetMessageRead(false,MessageActivity.DDTX_TAG);
                }else if (MessageActivity.GGTZ_TAG.equals(card.getDataEntity().getType()))
                {
                    Intent intent=new Intent(mContext,MessageGongGaoActivity.class);
                    mContext.startActivity(intent);
                    TaskSetMessageRead(false,MessageActivity.GGTZ_TAG);
                }

            }
        });

    }
    private void TaskSetMessageRead(final boolean isloadmore,String type) {
        RequestServerManager.getInstanceDIY().handleMethodDIY(null, null, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_SetMessageRed, Constant.Action.Action_SetMessageRed, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_SetMessageRed,type, App.getUserBean().getUserid()+"", App.getUserBean().getSession_id()+""), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        card.getDataEntity().setUnread_nums("0");
                        card.getMaterialListView().getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
}
